package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.resonse.JsonResult;
import cn.tedu._05mvcboot02.common.resonse.StatusCode;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.UpdateDTO;
import cn.tedu._05mvcboot02.pojo.dto.UserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Api(tags = "用户功能模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")

public class UserController {
    //自动装配
    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     *第一题
     */
    @ApiOperation(value = "注册用户")
      @PostMapping("insert")
        public JsonResult insert(@RequestBody @Validated UserDTO userDTO){
          log.debug("userDTO"+userDTO);
       //插入数据使用实体类
       User user = new User();
       //复制属性:把 userInsertDTO中属性值赋值给 user对象中
       BeanUtils.copyProperties(userDTO,user);
      user.setCreated(new Date());
       //调用接口方法
      userMapper.insertUser(user);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    /**
     * 第二题 查询用户列表
     */
    @ApiOperation(value = "用户列表")
    @GetMapping("userList")
    public JsonResult userList(){
        List<UserListVO> userListVOS = userMapper.userList();
        return new JsonResult(StatusCode.OPERATION_SUCCESS,userListVOS);
    }

    /**
     * 第三题 删除指定用户功能
     */
     @ApiOperation(value = "删除指定用户")
    @GetMapping("delete")
    public JsonResult deleteUser(int id){
         if (id<0){
             throw new IllegalArgumentException("ID值是无效的");
         }
        /**
         * 1.删除指定用户[接口]
         * 2.返回响应
         */
       userMapper.deleteUser(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    /**
     * 第四题 更新用户功能
     */
    @ApiOperation(value = "更新用户")
    @PostMapping("update")
    public JsonResult updateUser(UpdateDTO updateDTO){
        log.debug("updateDTO"+updateDTO);
        userMapper.updateUser(updateDTO);

        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation(value = "查询指定用户")
    @GetMapping("selectById/{id}")
    @ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "int")
    public JsonResult selectById(@PathVariable int id){
        UserListVO userListVO = userMapper.selectById(id);
      return  new JsonResult(StatusCode.OPERATION_SUCCESS,userListVO);

    }
}
